import { createRouter, createWebHashHistory } from 'vue-router';
import store from '@/store';

import ArticleCreateRouter from './modules/ArticleCreate';
import ArticleRouter from './modules/Article';
import PermissionListRouter from './modules/PermissionList';
import RoleListRouter from './modules/RoleList';
import UserManageRouter from './modules/UserManage';

export const asyncRoutes = [
  RoleListRouter,
  UserManageRouter,
  PermissionListRouter,
  ArticleCreateRouter,
  ArticleRouter
];

export const publicRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index.vue')
  },
  {
    path: '/',
    redirect: '/profile',
    component: () => import('@/layout/index.vue'),
    children: [
      // 个人中心
      {
        path: '/profile',
        name: 'profile',
        component: () => import('@/views/profile/index.vue'),
        meta: {
          title: 'profile',
          icon: 'el-icon-user'
        }
      },
      // 404
      {
        path: '/404',
        name: '404',
        component: () => import('@/views/error-page/404')
      },
      // 401
      {
        path: '/401',
        name: '401',
        component: () => import('@/views/error-page/401')
      }
    ]
  }
];

const router = createRouter({
  history: createWebHashHistory(),
  routes: publicRoutes
});

// 初始化路由表
export function resetRouter() {
  if (
    store.getters.userInfo &&
    store.getters.userInfo.permission &&
    store.getters.userInfo.permission.menus
  ) {
    const menus = store.getters.userInfo.permission.menus;
    menus.forEach((item) => {
      router.removeRoute(item);
    });
  }
}

export default router;
